/*
*  Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
*/
package com.pitop.box.ctrl;


import com.alibaba.fastjson.JSON;
import com.pitop.box.entity.BoxItem;
import com.pitop.box.service.BoxItemSVImpl;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.redis.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 装箱产品 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/boxItem")
@Slf4j
@Api(value = "装箱产品控制器", description = "装箱产品控制器")
public class BoxItemCtrl {

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private BoxItemSVImpl boxItemSVImpl;


    /**
     * 查询BoxItem一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询BoxItem一个详情信息", notes = "查询BoxItem一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id) {
        if (id == null) {
            return null;
        }
        BoxItem boxItem = boxItemSVImpl.load(id);
        log.info(JSON.toJSONString(boxItem));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, boxItem);
    }


    /**
     * 根据条件id查询BoxItem一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询BoxItem一个详情信息", notes = "查询BoxItem一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        BoxItem boxItem = boxItemSVImpl.loadById(id);
        log.info(JSON.toJSONString(boxItem));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, boxItem);
    }


    /**
     * 查询BoxItem信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询BoxItem信息集合", notes = "查询BoxItem信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),

    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore BoxItem boxItem, @ApiIgnore Page<BoxItem> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<BoxItem> boxItems = boxItemSVImpl.list(boxItem, page.getCurPage(), page.getPageSize());
        int total = boxItemSVImpl.count(boxItem);
        page.setTotalRow(total);
        page.setVoList(boxItems);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }


    /**
     * 查询BoxItem信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询BoxItem信息集合", notes = "查询BoxItem信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, @ApiIgnore Page<BoxItem> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<BoxItem> boxItems = boxItemSVImpl.list(id, page.getCurPage(), page.getPageSize());
        int total = boxItemSVImpl.count(id);
        page.setTotalRow(total);
        page.setVoList(boxItems);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 创建BoxItem
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建BoxItem", notes = "创建BoxItem")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "testUnitCode", value = "测试件编码", paramType = "query"),
            @ApiImplicitParam(name = "factoryCode", value = "工厂编码", paramType = "query"),
            @ApiImplicitParam(name = "boxSn", value = "箱体SN码", paramType = "query"),
            @ApiImplicitParam(name = "itemSn", value = "产品SN码", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore BoxItem boxItem) {
        if (StringUtils.isEmpty(boxItem.getTestUnitCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(boxItem.getFactoryCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(boxItem.getBoxSn())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(boxItem.getItemSn())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        boxItemSVImpl.save(boxItem);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, boxItem);
    }


    /**
     * 修改BoxItem
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改BoxItem", notes = "修改BoxItem")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "testUnitCode", value = "测试件编码", paramType = "query"),
            @ApiImplicitParam(name = "factoryCode", value = "工厂编码", paramType = "query"),
            @ApiImplicitParam(name = "boxSn", value = "箱体SN码", paramType = "query"),
            @ApiImplicitParam(name = "itemSn", value = "产品SN码", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore BoxItem boxItem) {
        if (StringUtils.isEmpty(boxItem.getTestUnitCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(boxItem.getFactoryCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(boxItem.getBoxSn())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(boxItem.getItemSn())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        boxItemSVImpl.modify(boxItem);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, boxItem);
    }

    /**
     * 根据主键删除BoxItem
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除BoxItem", notes = "删除BoxItem")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id) {
        if (id != null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }

        boxItemSVImpl.delete(id);
        return BeanRet.create(true, "删除BoxItem成功");
    }


    /**
     * 查询箱体内所有的产品信息
     *
     * @return BeanRet
     */
    @ApiOperation(value = "查询箱体内所有的产品信息", notes = "查询箱体内所有的产品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "箱SN", paramType = "query", required = true)
    })
    @GetMapping("/getSampleByBoxSn")
    @ResponseBody
    public BeanRet getSampleByBoxSn(String sn) {
        if (StringUtils.isEmpty(sn)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<BoxItem> boxItems = boxItemSVImpl.getSampleByBoxSn(sn);
        return BeanRet.create(true, "成功查询出箱体内产品信息", boxItems);
    }


}
